using System;
using System.Collections.Generic;
using WebAuth.Bussiness;

namespace WebAuth.Models
{
    public class AuthBinder : AuthModel
    {
        public Department Department { get; set; }
        public Role Role { get; set; }
        public string Derive { get; set; }

        internal List<Department> GetDeriveDepartments()
        {
            var res = new List<Department>();
            if (Derive.Equals("up"))
            {
                getParentDepartments(Department.Parent, res);
            }
            else if (Derive.Equals("down"))
            {
                getChildrenDepartments(Department.Id, res);
            }
            return res;
        }

        private void getChildrenDepartments(string id, List<Department> childrenDepartments)
        {
            CacheManager.GetCacheManager().GetModelSet(out Dictionary<string, Department> set);
            foreach (var department in set.Values)
            {
                if (department.Parent.Equals(id))
                {
                    childrenDepartments.Add(department);
                    getChildrenDepartments(department.Id,childrenDepartments);
                }
            }
        }

        void getParentDepartments(string parentId, List<Department> parentDepartments)
        {
            CacheManager.GetCacheManager().GetModelById(parentId, out Department parentDepartment);
            parentDepartments.Add(parentDepartment);
            if (!string.IsNullOrEmpty(parentDepartment.Parent))
            {
                getParentDepartments(parentDepartment.Parent, parentDepartments);
            }


        }

        /*public AuthBinder(string departmentId,string characterId, List<AuthGroup> authGroups)
        {
            DepartmentId = departmentId;
            CharacterId = characterId;
        }*/

        /*public void AddAuthGroup(AuthGroup authGroup)
        {
            AuthGroups.Add(authGroup);
        }*/



    }
}